import {
  isLogin
} from '@/api/index'
const login = async function (el) {
  const is_weixin = (function () {
    return (
      navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1
    );
  })();
  await isLogin().then(results => {
    if (is_weixin) {
      //是在微信浏览器中打开
      el.$store.commit('SETOPENID', results.data.openid)
      el.is_weixin = true;
      const oldUrl = window.location.href //获取地址栏地址
      if (!oldUrl.includes("code=")) { //如果没有code参数
        //判断地址栏是否有code参数，没有code参数，请求微信接口
        el.fristEnter = true;
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6a3ba177ec885136&redirect_uri=" + encodeURIComponent(oldUrl) + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
        return false;
      }
    } else {
      console.log("不是在微信浏览器打开")
      //不在微信浏览器中打开
      // el.$store.commit('SETOPENID', results.data)
      el.is_weixin = false;
      el.$store.commit('SETLOGINTYPE', 'h5')
      return false;
    }
  })

}
export default login
